<?php

namespace app\admin\model\customer;

use app\admin\model\Admin;
use think\Model;


class Deal extends Model
{


    // 表名
    protected $name = 'customer';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'admin_info',
        'start_time_text',
        'end_time_text',
        'customer_service',
    ];


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y年m月d日", $value) : $value;
    }

    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y年m月d日", $value) : $value;
    }


    public function getAdminInfoAttr($value, $data)
    {
        return Admin::get($data['admin_id']);
    }

    public function getCustomerServiceAttr($value, $data)
    {
        return (new Service)->where(['customer_id' => $data['id']])->select();
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
